var gamepadActive = false,        // whether a gamepad is connected or not
    ps3Buttons    = new Array(),  // an array of button id's which map to the button names
    keys          = new Array();  // an array of key "keyCodes" that map to the gamepad button id's
    ps3Axis       = new Array();

// building the button ID's to the button names
ps3Buttons[12]  = 'triangle',
ps3Buttons[15]  = 'square',
ps3Buttons[14]  = 'cross',
ps3Buttons[13]  = 'circle',
ps3Buttons[4]   = 'top',
ps3Buttons[7]   = 'left',
ps3Buttons[6]   = 'bottom',
ps3Buttons[5]   = 'right',
ps3Buttons[10]  = 'L1',
ps3Buttons[8]   = 'L2',
ps3Buttons[11]  = 'R1',
ps3Buttons[9]   = 'R2',
ps3Buttons[1]   = 'L3',
ps3Buttons[2]   = 'R3',
ps3Buttons[16]  = 'PS',
ps3Buttons[0]   = 'select',
ps3Buttons[3]   = 'start';

// the mapped keyCode's to gamepad button ID's
keys[38] = 4; // up
keys[37] = 7; // left
keys[40] = 6; // down
keys[39] = 5; // right

//Map Axis
ps3Axis[0]      = 'x';
ps3Axis[1]      = 'y';
ps3Axis[3]      = 'x';
ps3Axis[4]      = 'y';

// sets the gamepad connection state to true
function gamepadConnected(evt)
{
  console.log(evt);
  gamepadActive = true;
}

// sets the gamepad connection state to false
function gamepadDisconnected(evt)
{
  // console.log(evt);
  gamepadActive = false;
}

// when a keyboard or gamepad button is pressed, this is called
// it sets whether a button is being pressed to the player object
// the pressed state alters based on whether a key is pressed or released
// e.g. up button/key is pressed, player.up = true/false
function buttonPressed(evt, pressed)
{
  console.log(evt.button, pressed);
  //player[ps3Buttons[evt.button]] = pressed ? true : false;
  var button = getKeyPS3(ps3Buttons[evt.button]);
  button.state = pressed ? true : false;
  button.trigger = !pressed ? true : false;
  log('test');
}

//Contrôle le type de stick qui est déplacé et la fonction associée
function moveAnalogueSticks(evt) {
  switch(evt.axis){
    case 0 :  
    case 1 :  moveAxis(ps3Axis[evt.axis], evt.value);
    break;
  }
}

function moveAxis(axis, value){

  switch(axis){
    case 'x' :  var right = getKeyPS3(ps3Buttons['right']);
                right.trigger = (value > 0.01) ? true : false;
                var left = getKeyPS3(ps3Buttons['left']);
                left.trigger = (value < -0.01) ? true : false;
    break;
    case 'y' :  var bottom = getKeyPS3(ps3Buttons['top']);
                bottom.trigger = (value > 0.01) ? true : false;
                var top = getKeyPS3(ps3Buttons['bottom']);
                top.trigger  = (value < -0.01) ? true : false;
    break;
  }
  //log(axis);

}
// event listeners, self explanatory
window.addEventListener('MozGamepadConnected', gamepadConnected);
window.addEventListener('MozGamepadDisconnected', gamepadDisconnected);
window.addEventListener("MozGamepadButtonDown", function(evt) { buttonPressed(evt, true); } );
window.addEventListener("MozGamepadButtonUp", function(evt) { buttonPressed(evt, false); } );
window.addEventListener("MozGamepadAxisMove", moveAnalogueSticks);

// if a gamepad is not in use
// prevent the default action of the key and then use the "keys"
// mapping array to send through the gamepad button ID
/*window.onkeydown = function(evt)
  {
    if (gamepadActive == false)
    {
      evt.preventDefault();
      buttonPressed({ button: keys[evt.keyCode] }, true);
    }
  }
window.onkeyup = function(evt)
  {
    if (gamepadActive == false)
    {
      evt.preventDefault();
      buttonPressed({ button: keys[evt.keyCode] }, false);
    }
  }*/